package com.giscloud.amp.service.auth.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.giscloud.amp.dao.AmpPwChgTypeMapper;
import com.giscloud.amp.entity.AmpPwChgType;
import com.giscloud.amp.entity.AmpPwChgTypeExample;
import com.giscloud.amp.entity.dto.AmpPwChgTypeDTO;
import com.giscloud.amp.entity.vo.AmpPwChgTypeVO;
import com.giscloud.amp.service.auth.AmpPwChgTypeService;
import com.giscloud.datasource.page.PageInfo;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author:gxd
 * @Description:
 * @Date: 11:04 2018/2/24
 * @Modified By:
 */
@Service("ampPwChgTypeService")
@Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
public class AmpPwChgTypeServiceImpl implements AmpPwChgTypeService {

    private final static Logger log = LoggerFactory.getLogger(AmpPwChgTypeServiceImpl.class);
    @Autowired
    private AmpPwChgTypeMapper ampPwChgTypeMapper;

    @Override
    public PageInfo getAmpPwChgTypeList(AmpPwChgTypeDTO ampPwChgTypeDTO) {
        log.debug("#getAmpPwChgTypeList parameter ={}", JSON.toJSONString(ampPwChgTypeDTO));
        PageHelper.startPage(ampPwChgTypeDTO.getPage(), ampPwChgTypeDTO.getRows());

        AmpPwChgTypeExample ampPwChgTypeExample = new AmpPwChgTypeExample();
        AmpPwChgTypeExample.Criteria criteria = ampPwChgTypeExample.createCriteria();
        if(StringUtils.isNotBlank(ampPwChgTypeDTO.getName())){
            criteria.andNameLike("%"+ampPwChgTypeDTO.getName()+"%");
        }
        List<AmpPwChgTypeVO> ampPwChgTypes = ampPwChgTypeMapper.selectByExample(ampPwChgTypeExample);
        return PageInfo.of(ampPwChgTypes);
    }

    @Override
    public AmpPwChgTypeVO getAmpPwChgTypeByCondition(AmpPwChgTypeDTO ampPwChgTypeDTO) {
        log.debug("#getAmpPwChgTypeByCondition parameter ={}", JSON.toJSONString(ampPwChgTypeDTO));
        AmpPwChgTypeExample ampPwChgTypeExample = new AmpPwChgTypeExample();
        AmpPwChgTypeExample.Criteria criteria = ampPwChgTypeExample.createCriteria();

        List<AmpPwChgTypeVO> ampPwChgTypes = ampPwChgTypeMapper.selectByExample(ampPwChgTypeExample);
        if(!CollectionUtil.isEmpty(ampPwChgTypes) && ampPwChgTypes.size() == 1){
            return ampPwChgTypes.get(0);
        }else{
            return  null;
        }
    }
    @Override
    public int insert(AmpPwChgTypeDTO ampPwChgTypeDTO) {
        AmpPwChgType ampPwChgType = new AmpPwChgType();
        BeanUtils.copyProperties(ampPwChgTypeDTO,ampPwChgType);
        ampPwChgType.setCreatedDt(new Date());
        ampPwChgType.setVersion(new Date());
        return ampPwChgTypeMapper.insert(ampPwChgType);
    }
    @Override
    public int update(AmpPwChgTypeDTO ampPwChgTypeDTO) {
        AmpPwChgTypeExample ampPwChgTypeExample = new AmpPwChgTypeExample();
        AmpPwChgTypeExample.Criteria criteria = ampPwChgTypeExample.createCriteria();
        if(ampPwChgTypeDTO.getPwChgTypeId() != null){
            criteria.andPwChgTypeIdEqualTo(ampPwChgTypeDTO.getPwChgTypeId());
        }
        AmpPwChgType ampPwChgType = new AmpPwChgType();
        BeanUtils.copyProperties(ampPwChgTypeDTO,ampPwChgType);
        ampPwChgType.setCreatedDt(new Date());
        ampPwChgType.setVersion(new Date());
        return ampPwChgTypeMapper.updateByExample(ampPwChgType,ampPwChgTypeExample);
    }

    @Override
    public int delete(Integer pwChgTypeId) {
        AmpPwChgTypeExample ampPwChgTypeExample = new AmpPwChgTypeExample();
        AmpPwChgTypeExample.Criteria criteria = ampPwChgTypeExample.createCriteria();
        if(pwChgTypeId != null){
            criteria.andPwChgTypeIdEqualTo(pwChgTypeId);
        }
        return ampPwChgTypeMapper.deleteByExample(ampPwChgTypeExample);
    }

}
